package com.msc.cili.web.viewcontroller.subscribecontroller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.msc.cili.common.api.Result;
import com.msc.cili.repository.entity.subscribe.SubRecord;
import com.msc.cili.repository.entity.subscribe.Subscribe;
import com.msc.cili.service.subscribe.SubRecordService;
import com.msc.cili.service.subscribe.SubscribeService;
import com.msc.cili.service.user.UserService;

import com.msc.cili.web.annotation.VisitLogger;
import com.msc.cili.web.utils.ShiroUtil;
import com.msc.cili.web.viewcontroller.subscribecontroller.vo.SubscribeVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author masc
 * @date 2022/6/25 16:49
 * <p>订阅信息</p>
 */
@RestController
@RequestMapping("/sub")
public class SubscribeInfoViewController {
    @Resource
    private UserService userService;
    @Resource
    private SubscribeService subscribeService;
    @Resource
    private SubRecordService subRecordService;


    /**
     * @param
     * @return
     * @Author masc
     * @Description: 获取所有的可订阅列表
     * @Date 2022/6/25 20:49
     */
    @VisitLogger(behavior = "访问页面", content = "所有可订阅")
    @RequestMapping("/getSubscribes")
    public Result getSubscribes(@RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                @RequestParam(value = "typeId", defaultValue = "") Integer typeId) {
        IPage<Subscribe> pageList = subscribeService.getAllScribeList(typeId, pageIndex, pageSize);
        return Result.succ(pageList);
    }

    /**
     * @return com.msc.common.lang.Result
     * @Author masc
     * @Description: 确认订阅
     * @Date 2022/6/28 16:55
     */
//    @RequiresPermissions("user:create")
//    @RequiresAuthentication
//    @VisitLogger(behavior = "访问页面", content = "订阅")
//    @RequestMapping("/buy/subscribe")
//    public Result subscribe(@RequestBody SubscribeVo subscribeVo) {
//        if (null != ShiroUtil.getProfile()) {
//            Long userId = ShiroUtil.getProfile().getId();
//            boolean succ = subscribeService.buySubscribe(subscribeVo);
//            return Result.succ(succ);
//        }
////        IPage<Subscribe> pageList = subscribeService.getAllScribeList(typeId, pageIndex, pageSize);
////        return Result.succ(pageList);
//        return Result.succ(null);
//    }

    /**
     * @param
     * @Author masc
     * @Description: 获取所有的列表, 排除（3）已删除的
     * @Date 2022/6/25 20:49
     * @returna
     */
//    @RequiresPermissions("user:source")
//    @RequestMapping("/allSubscribeList")
//    public Result allSubscribeList(@RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
//                                   @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                   @RequestParam(value = "title",defaultValue = "") String title) {
//        QueryWrapper<Subscribe> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().ne(Subscribe::getStatus, 3);
//        if(StringUtils.isNotEmpty(title)){
//            queryWrapper.lambda().like(Subscribe::getTitle,title);
//        }
//        Page page = new Page(pageIndex, pageSize);
//        IPage<Subscribe> pageList = subscribeService.page(page, queryWrapper);
//        return Result.succ(pageList);
//    }

    /**
     * @param
     * @return com.msc.common.lang.Result
     * @Author masc
     * @Description: 保存或修改可订阅内容
     * @Date 2022/6/25 21:17
     */
//    @RequiresPermissions("user:create")
//    @RequiresAuthentication
//    @PostMapping("/subscribe/saveOrUpdate")
//    public Result saveOrUpdate(@RequestBody Subscribe subscribe) {
//        Subscribe temp = null;
//        if (subscribe.getId() != null) {
//            temp = subscribeService.getById(subscribe.getId());
//        } else {
//            temp = new Subscribe();
//            temp.setUserId(ShiroUtil.getProfile().getId());
//            temp.setCreateTime(LocalDateTime.now());
//        }
//        temp.setUpdateTime(LocalDateTime.now());
//        if( !StringUtils.isEmpty(subscribe.getSecret())){
//            BeanUtil.copyProperties(subscribe, temp, "id", "userId", "createTime", "updateTime");
//        }else{
//            BeanUtil.copyProperties(subscribe, temp, "secret","id", "userId", "createTime", "updateTime");
//        }
//
//        boolean b = subscribeService.saveOrUpdate(temp);
//        return Result.succ(b);
//    }

    /**
     * 修改订阅次数
     */
//    @RequiresPermissions("user:update")
//    @RequestMapping("/updateTimes/{id}")
//    @RequiresPermissions("user:create")
//    @RequiresAuthentication
//    public Result updateTimes(@PathVariable(name = "id") String id) {
//        Subscribe subscribe = subscribeService.getById(id);
//        Integer subTimes = subscribe.getSubTimes();
//        //将次数加一操作
//        subscribe.setSubTimes(subTimes + 1);
//        boolean succ = subscribeService.saveOrUpdate(subscribe);
//        return Result.succ(succ);
//    }

    /**
     * 查看详情
     */
//    @RequiresPermissions("user:source")
//    @VisitLogger(behavior = "查看详情")
//    @GetMapping("/subscribe/detail/{id}")
//    public Result getDetail(@PathVariable(name = "id") Long id, HttpServletRequest request) {
//        Subscribe subscribe = subscribeService.getById(id);
//        //查询该用户有无购买记录
//        SubRecord subRecord = null;
//        if (null != ShiroUtil.getProfile()) {
//            Long userId = ShiroUtil.getProfile().getId();
//            QueryWrapper<SubRecord> queryWrapper = new QueryWrapper<>();
//            queryWrapper.lambda().eq(SubRecord::getUserId, userId).eq(SubRecord::getSubscribeId, id);
//            subRecord = subRecordService.getOne(queryWrapper);
//        }
//        SubscribeVo subscribeVo = new SubscribeVo();
//
//        BeanUtil.copyProperties(subscribe, subscribeVo);
//        if (null != subRecord) {
//            subscribeVo.setHavSub(true);
//            return Result.succ(subscribeVo);
//        } else {
//            subscribeVo.setHavSub(false);
//            subscribeVo.setSecret("订阅后方可查看哦！");
//            return Result.succ(subscribeVo);
//        }
//    }

    /**
     * 修改资源的可见性
     * 状态 1可见，2不可见，3已删除
     */
//    @RequiresPermissions("user:create")
//    @RequiresAuthentication
//    @RequestMapping("sub/pushGoods/{id}")
//    public Result pushSubscribe(@PathVariable(name = "id") String id) {
//        Subscribe subscribe = subscribeService.getById(id);
//        if (subscribe.getStatus() == 0) {
//            subscribe.setStatus(1);
//        } else {
//            subscribe.setStatus(0);
//        }
//        boolean succ = subscribeService.saveOrUpdate(subscribe);
//        return Result.succ(succ);
//    }

    /**
     * 删除 状态 1可见，2不可见，3已删除
     */
//    @RequiresPermissions("user:create")
//    @RequiresAuthentication
//    @RequestMapping("sub/delete/{id}")
//    public Result deleteSubscribe(@PathVariable(name = "id") String id) {
//        Subscribe subscribe = subscribeService.getById(id);
//        subscribe.setStatus(3);
//        boolean succ = subscribeService.saveOrUpdate(subscribe);
//        return Result.succ(succ);
//    }
}
